<?php

namespace app\index\controller;

use think\Controller;
//app就是值application目录
use app\index\model\StudentModel;
//获取前端传过来的数值
use think\request;

class StudentController extends Controller
{
    /**查询列表
     * @return mixed
     */
    public function index(){
        //获取model
        $model = new StudentModel();
        $to = $model->index();

//        return json($to);

        //不用转json，直接打印的
//        var_dump( $to );

        $this->assign( 'list' , $to );
        //渲染处view中的html文件
        return $this->fetch('student\index');
    }

    /**显示添加页面
     * @return mixed
     */
    public function add(){
        return $this->fetch();
    }

    /**添加功能操作
     * @param request $request
     * @return void
     */
    public function adds(Request $request){
        $data = [
            'name' => $request->name,
            'sex' => $request->sex,
            'classes'=>$request->class,
            'age'=>$request->age
        ];

        $model = new StudentModel();
        $to = $model->adds($data);
        //$to=1添加成功； 0失败
        if($to){
            return $this->success('success to add !!!!' , 'index' );
        }
        return $this->error('error to add !!!!' );
    }

    /**点击修改按钮后，回显数据
     * @param request $request
     * @return mixed
     */
    public function edit(Request $request){
        $id = $request->id;
        $model = new StudentModel();
        $to = $model->edit( $id );
        $this->assign( 'datas' , $to );
        return $this->fetch();
    }

    public function edits(Request $request){
        $data = [
            'id' => $request->id,
            'name' => $request->name,
            'sex' => $request->sex,
            'class' => $request->classes,
            'age' => $request->age
        ];
        $model = new StudentModel();
        $to = $model->edits( $data );
        if( $to ){
            return $this->success( 'success to edit!!!!' , 'index' );
        }
        return $this->error( 'error to edit --__--!!!! ' );
    }

    /**实现删除功能
     * @param request $request
     * @return void
     */
    public function del(Request $request){
        $id = $request->id;
        $model = new StudentModel();
        $to = $model->del($id);
        if( $to ){
            return $this->success( 'success to del !!! ' , 'index' );
        }
        return $this->error( 'error to dell ---___--- !!! ' );
    }
}